// 导入数据库操作模块
const db = require('../db/index')
// 导入全局的配置文件
const config = require('../config')
// 导入 bcryptjs 这个包
const bcrypt = require('bcryptjs')

exports.userinfo = (req, res) => {
    const sqlstr = 'select id ,username,nickname,email,user_pic from ev_users where id = ?'
    db.query(sqlstr, req.user.id, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('获取用户信息失败')
        res.send({
            status: 0,
            message: '获取用户基本信息成功！',
            data: results[0]
        })
    })
    // res.cc('用户信息查询成功')
}
exports.menusInfo = (req, res) => {
    // console.log(req);
    res.send({
        status: 0,
        message: '获取侧边栏信息成功！',
        data: [
            {
                "indexPath": "/home",
                "title": "首页",
                "icon": "el-icon-s-home",
                "children": null
            },
            {
                "indexPath": "2",
                "title": "职位管理",
                "icon": "el-icon-s-order",
                "children": [
                    {
                        "indexPath": "/art-cate",
                        "title": "职位分类",
                        "icon": "el-icon-s-data"
                    },
                    {
                        "indexPath": "/art-list",
                        "title": "职位列表",
                        "icon": "el-icon-document-copy"
                    }
                ]
            },
            {
                "indexPath": "3",
                "title": "个人中心",
                "icon": "el-icon-user-solid",
                "children": [
                    {
                        "indexPath": "/user-info",
                        "title": "基本资料",
                        "icon": "el-icon-s-operation"
                    },
                    {
                        "indexPath": "/user-avatar",
                        "title": "更换头像",
                        "icon": "el-icon-camera"
                    },
                    {
                        "indexPath": "/user-pwd",
                        "title": "重置密码",
                        "icon": "el-icon-key"
                    }
                ]
            },// 新增的
			{
			    "indexPath": "4",
			    "title": "个人中心",
			    "icon": "el-icon-user-solid",
			    "children": [
			        {
			            "indexPath": "/user-weizhi",
			            "title": "基本资料",
			            "icon": "el-icon-s-operation"
			        },
			        {
			            "indexPath": "/user-daiding",
			            "title": "更换头像",
			            "icon": "el-icon-camera"
			        },
			        {
			            "indexPath": "/user-aa",
			            "title": "重置密码",
			            "icon": "el-icon-key"
			        }
			    ]
			}
        ],
    })
    // res.cc('用户信息查询成功')
}
// 更新用户信息
exports.updateUserInfo = (req, res) => {
    // update user set name=?,age=?,gennder=? where id = ?
    const sql = 'update ev_users set ? where id =?'
    db.query(sql, [req.body, req.body.id], (err, results) => {
        // 执行SQL语句失败
        if (err) return res.cc(err)
        // 执行sql语句成功，但是影响的行数不等于1
        if (results.affectedRows !== 1) return res.cc('更新失败')
        res.cc('更新成功', 0)
    })
    // res.send("更新成功")
}

exports.updatepwd = (req, res) => {
    const check = 'select * from ev_users where id = ?'
    db.query(check, req.user.id, (err, results) => {
        if (err) return res.cc(err)
        // console.log(req.user.id);
        if (results.length !== 1) return res.cc('用户不存在')
        // 判断提交的旧密码是否正确
        const compareresult = bcrypt.compareSync(req.body.oldpwd, results[0].password)
        if (!compareresult) {
            return res.cc('原密码错误，请重新输入！')
        }
        // res.cc('修改成功',0)
        // 调用 bcrypt.hashSync()对密码进行加密 两个参数：1要加密的密码 2加密的等级
        req.password = bcrypt.hashSync(req.body.newpwd, 10)
        console.log(req.password);
        //  update user set name=?,age=?,gennder=? where id = ?
        const sqlstr = 'update ev_users set  password= ? where id = ?'
        db.query(sqlstr, [req.password, req.user.id], (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('更新密码失败')
            res.cc('更新密码成功，请重新登录',0)
        })
    })
    // res.send('修改密码成功')
}

exports.updateimg = (req, res) => {
    // console.log('头像数据',req.body);
    const sql = 'update ev_users set user_pic=? where id = ?'
    db.query(sql, [req.body.avatar, req.user.id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('更换头像失败')
        res.cc('更换头像成功', 0)
    })
    // res.cc('ok',0)
}